<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
﻿<!DOCTYPE HTML>
<html>
<head>
<jsp:include page="common_header.jsp" flush="true"></jsp:include>
<title>用户列表</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 用户管理 <span class="c-gray en">&gt;</span> 用户列表 <a id="refreshBtn" class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
	<div class="text-c"> 创建时间：
		<input type="text" onfocus="WdatePicker({ maxDate:'#F{$dp.$D(\'datemax\')||\'%y-%M-%d\'}' })" id="datemin" class="input-text Wdate" style="width:120px;">
		-
		<input type="text" onfocus="WdatePicker({ minDate:'#F{$dp.$D(\'datemin\')}',maxDate:'%y-%M-%d' })" id="datemax" class="input-text Wdate" style="width:120px;">
		<input type="text" class="input-text" style="width:250px" placeholder="输入用户姓名、登录名" id="keywords" name="">
		<button type="button" onclick="queryUser()" class="btn btn-success" id="" name=""><i class="Hui-iconfont">&#xe665;</i> 搜用户</button>
	</div>
	<div class="cl pd-5 bg-1 bk-gray mt-20"> <span class="l"><a href="javascript:;" onclick="batchDel()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a> <a href="javascript:;" onclick="admin_add('添加管理员','${path}/user/toPage.do?page=admin-add','800','500')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加用户</a></span> <span class="r">共有数据：<strong id="totalRow">-</strong> 条</span> </div>
	<table id="userTbl" class="table table-border table-bordered table-bg">
		<thead>
			<tr>
				<th scope="col" colspan="9">员工列表</th>
			</tr>
			<tr class="text-c">
				<th width="25"><input type="checkbox" name="" value="" onclick="checkAll(this)"/></th>
				<th width="20">序号</th>
				<th width="70">登录名</th>
				<th width="50">姓名</th>
				<th width="90">上一次登陆IP</th>
				<th width="80">上一次登陆时间</th>
				<th width="80">创建时间</th>
				<th width="100">状态</th>
				<th width="100">操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
<jsp:include page="_footer.jsp" flush="true"></jsp:include>
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="${path }/lib/My97DatePicker/4.8/WdatePicker.js"></script> 
<script type="text/javascript">

$(function(){
	
	initDg('userTbl', {
		queryUrl : '${path}/user/query.do',
		beforeAjax : function(data){
			data.begin = $('#datemin').val();
        	data.end = $('#datemax').val();
        	data.keywords = $('#keywords').val();
		},
		sucessAjax : function(result){
			$('#totalRow').html(result.data.recordsTotal);
		},
		columns: [
            { "data": null },
            { "data": null },
            { "data": "username" },
            { "data": "fullName" },
            { "data": "lastLoginIp" },
            { "data": "lastLoginTime" },
            { "data": "createTime" },
            { "data": "status" },
            { "data": null }
        ],
        rowCallback : function(row, data, index){
        	$('td:eq(5)', row).html( fmtMillsec(data.lastLoginTime, true) );
    		$('td:eq(6)', row).html( fmtMillsec(data.createTime, true) );
    		var statusTxt = '';
    		var statusLabel = '';
    		if (data.status == '1') {
    			statusTxt = '<a style="text-decoration:none" onClick="admin_stop(this,'+data.id+')" href="javascript:;" title="停用"><i class="Hui-iconfont">&#xe631;</i></a>';
    			statusLabel = '<span class="label label-success radius">已启用</span>';
			} else if (data.status == '0'){
				statusTxt = '<a style="text-decoration:none" onClick="admin_start(this,'+data.id+')" href="javascript:;" title="启用"><i class="Hui-iconfont">&#xe615;</i></a>';
				statusLabel = '<span class="label radius">已停用</span>';
			}
    		$('td:eq(7)', row).html( statusLabel );
    		$('td:eq(7)', row).addClass('td-status');
    		$('td:eq(8)', row).addClass('td-manage');
    		$('td:eq(8)', row).html( statusTxt+' <a title="编辑" href="javascript:;" onclick="admin_edit(\'管理员编辑\',\'${path}/user/toPage.do?page=admin-add\','+data.id+',\'800\',\'500\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> <a title="删除" href="javascript:;" onclick="admin_del(this,'+data.id+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a><a title="重置并发送密码" href="javascript:;" onclick="admin_sendLoginInfo('+data.id+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont Hui-iconfont-email"></i></a>' );
        }
	});
	
});


function refresh(index){
	queryUser();
	layer.close(index);
}

function queryUser(){
// 	$('#userTbl thead input[type=checkbox]').each(function(){
// 		console.log(this.checked);
// 		$(this).prop("checked",false);
// 	});
	$("#userTbl").dataTable().fnDraw(false);
}



/*
	参数解释：
	title	标题
	url		请求的url
	id		需要操作的数据id
	w		弹出层宽度（缺省调默认值）
	h		弹出层高度（缺省调默认值）
*/
/*管理员-增加*/
function admin_add(title,url,w,h){
// 	layer_show(title,url,w,h);
	layer.open({
		type: 2,
		area: [w+'px', h +'px'],
		fix: false, //不固定
		maxmin: true,
		shade:0.4,
		title: title,
		content: url,
		success: function(layero, index){
		    var body = layer.getChildFrame('body', index);
		    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
		    iframeWin.initRoleData();
// 		    console.log(body.html()) //得到iframe页的body内容
// 		    body.find('input').val('Hi，我是从父页来的')
	    }
	});
}
/*管理员-删除*/
function admin_del(obj,id){
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'POST',
			url: '${path}/user/delete.do',
			data: 'ids='+id,
			dataType: 'json',
			success: function(data){
				if (data.code == 0) {
					layer.msg('已删除!',{icon:1,time:1000});
					queryUser();
				}else{
					layer.msg('删除失败!',{icon:1,time:1000});
				}
			},
			error:function(data) {
				console.log(data.msg);
			},
		});		
	});
}

function checkAll(t){
	var hasCheck = t.checked;
	var s = $(t).parent().parent().parent().parent().find('tbody input[type=checkbox]');
// 	alert(s.length);
	$('#userTbl tbody input[type=checkbox]').each(function(){
		if (hasCheck) {
			$(this).prop("checked",true);
		}else{
			$(this).prop("checked",false);
		}
	});	
}

function batchDel(){
	var idArr = [];
	$('#userTbl tbody :checked').each(function(){
		idArr.push($(this).next().val());
	});
	if (idArr.length > 0) {
		layer.confirm('确认要删除这'+idArr.length+'条记录吗？',function(index){
			$.ajax({
				url: '${path}/user/delete.do',
				data: 'ids='+idArr.join(','), 
				type: "POST",
				success: function(data, textStatus){
					if (data.code == 0) {
						layer.msg('已删除!',{icon:1,time:1000});
						queryUser();
					}else{
						layer.msg('删除失败!',{icon:1,time:1000});
					}
				},
				error: function (data, textStatus, errorThrown) {
					var s=JSON.stringify(data);
					//console.log(s);
				}
			});			
		});
	}else{
		layer.alert('请选择记录!',{icon:1,time:2000});
	}
}

/*管理员-编辑*/
function admin_edit(title,url,id,w,h){
// 	layer_show(title,url,w,h);
	layer.open({
		type: 2,
		area: [w+'px', h +'px'],
		fix: false, //不固定
		maxmin: true,
		shade:0.4,
		title: title,
		content: url,
		success: function(layero, index){
		    var body = layer.getChildFrame('body', index);
		    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
		    iframeWin.fillData(id);
// 		    console.log(body.html()) //得到iframe页的body内容
// 		    body.find('input').val('Hi，我是从父页来的')
	    }
	});
}
/*管理员-停用*/
function admin_stop(obj,id){
	layer.confirm('确认要停用吗？',function(index){
		$.ajax({
			url: "${path}/user/changeStatus.do",
			data: 'id='+id+'&status=0', 
			type: "POST",
			success: function(data, textStatus){
				if (data.code == 0) {
					$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_start(this,'+id+')" href="javascript:;" title="启用" style="text-decoration:none"><i class="Hui-iconfont">&#xe615;</i></a>');
					$(obj).parents("tr").find(".td-status").html('<span class="label label-default radius">已停用</span>');
					$(obj).remove();
					layer.msg('已停用!',{icon: 5,time:1000});
				}else{
					alert(data.msg);
				}
			},
			error: function (data, textStatus, errorThrown) {
				var s=JSON.stringify(data);
				//console.log(s);
			}
		});	
		
	});
}

/*管理员-启用*/
function admin_start(obj,id){
	layer.confirm('确认要启用吗？',function(index){
		$.ajax({
			url: "${path}/user/changeStatus.do",
			data: 'id='+id+'&status=1', 
			type: "POST",
			success: function(data, textStatus){
				if (data.code == 0) {
					$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_stop(this,'+id+')" href="javascript:;" title="停用" style="text-decoration:none"><i class="Hui-iconfont">&#xe631;</i></a>');
					$(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已启用</span>');
					$(obj).remove();
					layer.msg('已启用!', {icon: 6,time:1000});
				}else{
					alert(data.msg);
				}
			},
			error: function (data, textStatus, errorThrown) {
				var s=JSON.stringify(data);
				//console.log(s);
			}
		});		
		
		
	});
}

function admin_sendLoginInfo(id){
	layer.confirm('确认要重置该用户密码?', function(index){
		jajax({
			url:'${path}/user/sendLoginInfoMail.do',
			data: 'userId='+id, 
			type:'post',
			success:function(data){
				layer.msg('已重置并发送到邮箱,请及时查收!', {icon: 6,time:1500});
			}	
				
		});
		
	});
}


</script>
</body>
</html>
